package cqrtplm.controller;


import com.github.pagehelper.PageInfo;
import com.hustcad.plm.caddoc.service.impl.batchimport.TyplmCADBatchImportUtil;
import com.hustcad.plm.middle.caddocapi.service.TyplmDwgFileParseService;
import com.hustcad.plm.pdm.caddoc.model.dto.ImportXmlDTO;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.middle.caddoc.model.dto.Import2dDocForTsDTO;
import com.hustcad.plm.pdm.middle.caddoc.model.dto.Import2dDocForWebDTO;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.ty.basic.entity.cad.CADDocDO;
import com.ty.basic.entity.type.TypeDO;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.CadDataDTO;
import cqrtplm.dto.ProblemReportDTO;
import cqrtplm.mapper.StoragePlanningMapper;
import cqrtplm.service.FromDataService;
import cqrtplm.service.GetCADDocumentService;
import cqrtplm.util.RTLogHelper;
import cqrtplm.vo.CadDataVO;
import cqrtplm.vo.OnlineAnnotationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


@RestController
@Api(tags = "适途获取技术要求文档")
@RequestMapping(RouteConstant.PATH + "v1")
public class CADController {
    @Resource
    private GetCADDocumentService getCADDocumentService;
    @Resource
    private RTLogHelper rtLogHelper;
    @Resource
    private StoragePlanningMapper storagePlanningMapper;
    @Resource
    private TyplmTypeService typlmTypeService;

    @Resource
    private TyplmDwgFileParseService typlmDwgFileParseService;
    @Resource
    private TyplmCADBatchImportUtil typlmCADBatchImportUtil;
    @Resource
    private FromDataService fromDataService;

    @PostMapping("/getTechnologyDocument")
    @ApiOperation(value = "getTechnologyDocument", produces = "application/json; charset=utf-8")
    public void getTechnologyDocument(HttpServletResponse response, @RequestBody CadDataDTO cadDataDTO) {
        if (StringUtils.isNotBlank(cadDataDTO.getFileCode())) {
            this.getCADDocumentService.getTechnologyDocumentByNumber(cadDataDTO, response);
        } else {
            this.getCADDocumentService.getTechnologyDocumentByName(cadDataDTO, response);
        }
        rtLogHelper.insertIntoLog(String.valueOf(cadDataDTO), "", cadDataDTO.getUserName());
    }

    @PostMapping("/getDocumentList")
    @ApiOperation(value = "getDocumentList", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo<ResultEntityVO>> getDocumentList(@RequestBody CadDataDTO cadDataDTO) {
        List<CadDataVO> conditionDTO = this.getCADDocumentService.getConditionDTO(cadDataDTO);
        rtLogHelper.insertIntoLog(String.valueOf(cadDataDTO), String.valueOf(conditionDTO), cadDataDTO.getUserName());
        return ResponseResult.success(conditionDTO);
    }

    @PostMapping("refreshOnlineAnnotation")
    @ApiOperation(value = "refreshOnlineAnnotation", produces = "application/json; charset=utf-8")
    public ResponseResult<List<OnlineAnnotationVO>> refreshOnlineAnnotation(HttpServletRequest request,
                                                                            HttpServletResponse response,
                                                                            @RequestBody ProblemReportDTO dto) {


        List<OnlineAnnotationVO> onlineAnnotationDOS = getCADDocumentService.refreshOnlineAnnotation(request, response,
                                                                                                     dto);
        return ResponseResult.success(onlineAnnotationDOS);
    }


    /**
     * 获取最新版本的CAD文档状态
     *
     * @param import2dDocForTsDTO
     * @return ResponseResult<List<ResultEntityVO>>
     */
    @PostMapping("getLatestDocVersion")
    public ResponseResult<List<CADDocDO>> get2DDrawingStatus(Import2dDocForTsDTO import2dDocForTsDTO) {
        if (ObjectUtils.isEmpty(import2dDocForTsDTO)) {
            throw RTErrorCodeEnum.DOC_TYPE_ERROR.getException(
                    String.format("文件异常请重新上传图片"));
        }

        Import2dDocForWebDTO import2dDocForWebDTO = new Import2dDocForWebDTO();
        BeanUtils.copyProperties(import2dDocForTsDTO, import2dDocForWebDTO);

        if (!"Capital".equals(import2dDocForWebDTO.getSoftware())) {

            import2dDocForWebDTO.setNotAnalysisPartCode(import2dDocForWebDTO.getNotAnalysisCode());
            import2dDocForWebDTO.setNotAnalysisPartName(import2dDocForWebDTO.getNotAnalysisName());
        }

        String standXmlStr = typlmDwgFileParseService.dwgFileParse(import2dDocForWebDTO);
        ImportXmlDTO importXmlDTO = typlmCADBatchImportUtil.xml2Object(standXmlStr);

        if (ObjectUtils.isEmpty(importXmlDTO) || ObjectUtils.isEmpty(importXmlDTO.getDocList())) {
            throw RTErrorCodeEnum.DOC_TYPE_ERROR.getException(
                    String.format("参数异常%s", import2dDocForWebDTO.getNotAnalysisName()));
        }

        List<CADDocDO> docs = storagePlanningMapper.queryCadByNumberAndVersion(
                importXmlDTO.getDocList().get(0).getDlgGuid());

        for (CADDocDO doc : docs) {
            TypeDO typeDO = typlmTypeService.queryTypeByOID(doc.getTypeoid());
            doc.setDescription(typlmTypeService.queryTypeDisplayNameByName(typeDO.getName()));
        }

        return ResponseResult.success(docs);
    }

}
